﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ykglxt
{
    public partial class Form_YPCGJGFX : Form
    {
        public Form_YPCGJGFX()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlDataAdapter sdaYP;
        SqlDataAdapter sdaJGFX;
        DataSet ds;
        bool empty = true;
        int firsttime = 0;

        private void Form_YPCGJGFX_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            ds = new DataSet();
            sdaYP = new SqlDataAdapter("SELECT BM AS 药品编码, MC AS 药品名称, GG AS 规格, BZDW AS 单位, BZDJ AS 单价 " +
                                           "FROM Dic_YP WHERE BM IN (SELECT YPBM FROM ML_CRXX WHERE CR = 0) ORDER BY BM", con);
            sdaJGFX = new SqlDataAdapter("SELECT R1.YPBM, R0.RQ AS 入库日期, R1.SL AS 数量, R1.JJ AS 进价, R1.PJ AS 批价, R1.LSJ AS 零售价, GYS.MC AS 供应商 " +
                                  "FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM " +
                                  "WHERE R0.YKBM = '" + PublicUnit.UserKSBM + "' AND R0.RQ BETWEEN '" + Convert.ToDateTime(dtpBegin.Text).ToString().Trim() + "' AND '"
                                   + Convert.ToDateTime(dtpEnd.Text).ToString().Trim() + "'", con);
            try
            {
                sdaYP.Fill(ds, "YP");
                sdaJGFX.Fill(ds, "JGFX");
                dgvFXYP.DataSource = ds.Tables["YP"].DefaultView;
                dgvFXYP.Columns[2].Visible = false;
                dgvFXYP.Columns[3].Visible = false;
                dgvFXYP.Columns[4].Visible = false;
                
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            ds.Tables["JGFX"].PrimaryKey = new DataColumn[] { ds.Tables["JGFX"].Columns[0] };
        }

        private bool getData()
        {
            bool res;
            try
            {
                res = false;

                using (SqlConnection conn = new SqlConnection(PublicUnit.ConnetionString))
                {
                    try
                    {
                        conn.Open();
                        SqlDataAdapter da = new SqlDataAdapter("select bm as '编码',zjf as '助记码',mc AS '药品名称',gg AS '规格',dj as '单价',sccs AS '生产产家' from dic_yp where lx in (0,4)", conn);
                        da.Fill(ds, "YPDW");
                        this.dgvYPDW.DataSource = ds.Tables["YPDW"].DefaultView;
                        res = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error!" + ex.ToString());
                        res = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("error!" + ex.ToString());
                res = true;
            }
            return res;
        }

        private void tbxYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (empty)
                return;

            if (e.KeyCode == Keys.Down)
            {
                dgvYPDW.Focus();
                if (dgvYPDW.CurrentRow.Index + 1 < dgvYPDW.Rows.Count)
                {
                    dgvYPDW.CurrentCell = dgvYPDW[0, dgvYPDW.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                this.tbxYPDW.Text = this.dgvYPDW.CurrentRow.Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
            }
        }

        private void tbxYPDW_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.dgvYPDW.Top = this.tbxYPDW.Top + this.tbxYPDW.Height;

                if (firsttime == 0)
                {
                    firsttime = 1;
                    empty = getData();
                }

                if (empty)
                    return;

                if (tbxYPDW.Text == " ")
                {
                    if (ds.Tables["YPDW"].DefaultView.Count != 0)
                        dgvYPDW.Visible = true;

                }
                else
                {
                    string dh = "编码 like '" + tbxYPDW.Text.Trim() + "%' or 药品名称 like '%" + tbxYPDW.Text.Trim() + "%'" + " or 助记码 like '" + tbxYPDW.Text.Trim() + "%'";
                    ds.Tables["YPDW"].DefaultView.RowFilter = dh.ToString();
                }
                if (ds.Tables["YPDW"].DefaultView.Count != 0)
                {
                    dgvYPDW.Visible = true;
                    dgvYPDW.CurrentCell = dgvYPDW[0, 0];
                }

                if (ds.Tables[0].DefaultView.Count == 0)//找不到，模糊查询
                {
                    string dh = "编码 like '%" + tbxYPDW.Text.Trim() + "%'  ";
                    ds.Tables["YPDW"].DefaultView.RowFilter = dh.ToString();
                    if (ds.Tables["YPDW"].DefaultView.Count != 0)
                    {
                        dgvYPDW.Visible = true;
                        dgvYPDW.CurrentCell = dgvYPDW[0, 0];
                    }

                    if (ds.Tables["YPDW"].DefaultView.Count == 0)
                    {
                        dgvYPDW.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void tbxYPDW_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (tbxYPDW.ReadOnly) // 只读, 不处理 
                return;

            if ((int)e.KeyChar <= 32) // 特殊键(含空格), 不处理 
                return;

            if (!char.IsDigit(e.KeyChar) && !char.IsLetter(e.KeyChar)) // 非数字字母键, 放弃该输入 
            {
                e.Handled = true;
                return;
            }
        }

        private void dgvYPDW_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.tbxYPDW.Text = this.dgvYPDW.Rows[dgvYPDW.CurrentCell.RowIndex].Cells[0].Value.ToString();
            this.dgvYPDW.Visible = false;
            for (int i = 0; i < this.dgvFXYP.Rows.Count; i++)
            {
                if (tbxYPDW.Text == this.dgvFXYP.Rows[i].Cells[0].Value.ToString())
                {
                    this.dgvFXYP.ClearSelection();
                    this.dgvFXYP.Rows[i].Selected = true;
                    this.dgvFXYP.CurrentCell = this.dgvFXYP.Rows[i].Cells[0];
                    return;
                }
            }
        }

        private void dgvYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.tbxYPDW.Text = this.dgvYPDW.Rows[dgvYPDW.CurrentCell.RowIndex].Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
                for (int i = 0; i < this.dgvFXYP.Rows.Count; i++)
                {
                    if (tbxYPDW.Text == this.dgvFXYP[this.dgvFXYP.Columns[0].Index, this.dgvFXYP.Rows[i].Index].Value.ToString())
                    {
                        this.dgvFXYP.ClearSelection();
                        this.dgvFXYP.Rows[i].Selected = true;
                        this.dgvFXYP.CurrentCell = this.dgvFXYP.Rows[i].Cells[0];
                        return;
                    }
                }
            }
        }

        private void dgvFXYP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbxGG.Text = dgvFXYP.Rows[dgvFXYP.CurrentCell.RowIndex].Cells[2].Value.ToString().Trim();
            tbxDW.Text = dgvFXYP.Rows[dgvFXYP.CurrentCell.RowIndex].Cells[3].Value.ToString().Trim();
            tbxDQZXDJ.Text = dgvFXYP.Rows[dgvFXYP.CurrentCell.RowIndex].Cells[4].Value.ToString().Trim();
            ds.Tables["JGFX"].Rows.Find(dgvFXYP.Rows[dgvFXYP.CurrentCell.RowIndex].Cells[0].ToString().Trim());
            dgvJGFX.DataSource = ds.Tables["JGFX"].DefaultView;
            dgvJGFX.Columns[0].Visible = false;
            //图形操作
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            sdaYP = new SqlDataAdapter("SELECT BM AS 药品编码, MC AS 药品名称, GG AS 规格, BZDW AS 单位, BZDJ AS 单价 " +
                                           "FROM Dic_YP WHERE BM IN (SELECT YPBM FROM ML_CRXX WHERE CR = 0) ORDER BY BM", con);
            sdaJGFX = new SqlDataAdapter("SELECT R1.YPBM, R0.RQ AS 入库日期, R1.SL AS 数量, R1.JJ AS 进价, R1.PJ AS 批价, R1.LSJ AS 零售价, GYS.MC AS 供应商 " +
                                  "FROM ML_RKD0 R0 INNER JOIN ML_RKD1 R1 ON R0.RKDH = R1.RKDH LEFT JOIN Dic_GYS GYS ON R0.GYSBM = GYS.BM " +
                                  "WHERE R0.YKBM = '" + PublicUnit.UserKSBM + "' AND R0.RQ BETWEEN '" + Convert.ToDateTime(dtpBegin.Text).ToString().Trim() + "' AND '"
                                   + Convert.ToDateTime(dtpEnd.Text).ToString().Trim() + "'", con);
            try
            {
                sdaYP.Fill(ds, "YP");
                sdaJGFX.Fill(ds, "JGFX");
                dgvFXYP.DataSource = ds.Tables["YP"].DefaultView;
                dgvFXYP.Columns[2].Visible = false;
                dgvFXYP.Columns[3].Visible = false;
                dgvFXYP.Columns[4].Visible = false;
                dgvJGFX.DataSource = ds.Tables["JGFX"].DefaultView;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            ds.Tables["JGFX"].PrimaryKey = new DataColumn[] { ds.Tables["JGFX"].Columns[0] };
        }
    }
}
